const express = require('express')
const { body, validationResult } = require('express-validator')
const mongoose = require('mongoose')
const Product = require('./models/product.model')
const passportSetup = require('./config/passport-setup')
const User = require('./models/user.model.js')
const cookieSession = require('cookie-session')
const passport = require('passport')
require('dotenv').config()

const port = process.env.PORT
const app = express()

app.use(express.json());
app.use(express.static(__dirname + '/public'));
app.use(express.urlencoded({extended:true}))

app.use(cookieSession({
    maxAge:24*60*60*1000,
    keys:[process.env.COKIE_SESSION_KEYS]
}))

app.use(passport.initialize())
app.use(passport.session())

app.set('view engine', 'ejs')

app.get('/', (req, res) => {
    res.render("index.ejs", {user: req.user});
});

// AUTH DONE
const loginGoogle = require('./routes/google-auth.js')
app.use('/', loginGoogle)
// COMMENT TODO
const commentSys = require('./routes/comment.js')
app.use('/api/user', commentSys)
const profRoutes = require('./routes/profile.routes.js')
app.use('/profile', profRoutes)
// const apiRouter =  require("./routes/api.js")
// app.use('/api', apiRouter)

mongoose.connect(process.env.MONGODB_SRV)
.then(() =>{
    console.log("Connected to database");
})
.catch(() =>{
    console.log("Failed to connect to database");
});

app.listen(port, () =>{
    console.log('Server is running on port ' + port);
});
//https://www.youtube.com/watch?v=_7UQPve99r4 //crud api guide freecodecamp
//https://www.youtube.com/watch?v=SccSCuHhOw0 //expressjs fast guide
//https://www.youtube.com/watch?v=e-nZAsVw7Rw&list=PL4cUxeGkcC9jdm7QX143aMLAqyM-jTZ2x&index=17 //other fast guide about passport.js
